package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HaDrugPurchase;
import com.ruoyi.system.service.IHaDrugPurchaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单Controller
 * 
 * @author 鲍小伟
 * @date 2025-11-25
 */
@RestController
@RequestMapping("/system/purchase")
public class HaDrugPurchaseController extends BaseController
{
    @Autowired
    private IHaDrugPurchaseService haDrugPurchaseService;

    /**
     * 查询采购单列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(HaDrugPurchase haDrugPurchase)
    {
        startPage();
        List<HaDrugPurchase> list = haDrugPurchaseService.selectHaDrugPurchaseList(haDrugPurchase);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:export')")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HaDrugPurchase haDrugPurchase)
    {
        List<HaDrugPurchase> list = haDrugPurchaseService.selectHaDrugPurchaseList(haDrugPurchase);
        ExcelUtil<HaDrugPurchase> util = new ExcelUtil<HaDrugPurchase>(HaDrugPurchase.class);
        util.exportExcel(response, list, "采购单数据");
    }

    /**
     * 获取采购单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:query')")
    @GetMapping(value = "/{purchaseid}")
    public AjaxResult getInfo(@PathVariable("purchaseid") Long purchaseid)
    {
        return success(haDrugPurchaseService.selectHaDrugPurchaseByPurchaseid(purchaseid));
    }

    /**
     * 新增采购单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:add')")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HaDrugPurchase haDrugPurchase)
    {
//      return toAjax(haDrugPurchaseService.insertHaDrugPurchase(haDrugPurchase));
        HaDrugPurchase po = haDrugPurchaseService.insertHaDrugPurchase(haDrugPurchase);
        return success(po);   // 返回整个实体（含 purchaseid）
    }

    /**
     * 修改采购单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:edit')")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HaDrugPurchase haDrugPurchase)
    {
        return toAjax(haDrugPurchaseService.updateHaDrugPurchase(haDrugPurchase));
    }

    /**
     * 删除采购单
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:remove')")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseids}")
    public AjaxResult remove(@PathVariable Long[] purchaseids)
    {
        return toAjax(haDrugPurchaseService.deleteHaDrugPurchaseByPurchaseids(purchaseids));
    }
}
